package com.datamation.swadeshi.view;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.CustomerAdapter;
import com.datamation.swadeshi.adapter.SetTitleAdapter;
import com.datamation.swadeshi.control.AlertBox;
import com.datamation.swadeshi.control.ReferenceNum;
import com.datamation.swadeshi.control.Toast;
import com.datamation.swadeshi.control.data.DebtorDS;
import com.datamation.swadeshi.control.data.InvHedDS;
import com.datamation.swadeshi.control.data.RouteDS;
import com.datamation.swadeshi.control.data.SharedPreferencesClass;
import com.datamation.swadeshi.model.Debtor;
import com.datamation.swadeshi.model.InvHed;
import com.datamation.swadeshi.model.OrdHed;

public class FragmentSalesManagementVanSaleCustomer extends Fragment implements OnClickListener{
	View view;
	ListView lvCustomers;
	
	ArrayList<Debtor> customerList;
	private DebtorDS dsDebtorDS;
	Debtor debtor;
	CustomerAdapter customerAdapter;
	Button allCust, routeCust; 
	
	Toast  toast;
	
	//Shared Preferences variables 
	public static final String SETTINGS = "SETTINGS";
	public static SharedPreferences localSP;
	
	boolean isRouteWise =false;
	
	
	@SuppressWarnings("deprecation")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.sales_management_van_sales_customer, container, false);
		toast = new Toast();
		
		setHasOptionsMenu(true);
		
		allCust =(Button) view.findViewById(R.id.all_cust);
		routeCust =(Button) view.findViewById(R.id.route_wise_cust);
		
		allCust.setOnClickListener(this);
		routeCust.setOnClickListener(this);
		
		changeButtonRouteCustomerOnPress();
		restButtonAllCustomerOnRelease();
		
		SetTitleAdapter adapter =new SetTitleAdapter();
		adapter.createCutomActionBarTitle("Customer - Van", getActivity());
		
		MainActivity activity = (MainActivity) getActivity();

		localSP = activity.getSharedPreferences(SETTINGS,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
		
		ReferenceNum referenceNum = new  ReferenceNum(getActivity());
		//orderNo.setText(referenceNum.getCurrentRefNo("EXPR"));
		InvHedDS invHedDS =new InvHedDS(getActivity());
		ArrayList<InvHed> getinvHed = invHedDS.getAllActiveInvhed(referenceNum.getCurrentRefNo(getResources().getString(R.string.VanNumVal)));

		if(!getinvHed.isEmpty()){
			
			for (InvHed invHed : getinvHed) {
				
				if(activity.selectedDebtor == null){
					DebtorDS debtorDS =new DebtorDS(getActivity());
					activity.selectedDebtor=debtorDS.getSelectedCustomerByCode(invHed.getFINVHED_DEB_CODE());
					activity.cusPosition= Integer.parseInt(localSP.getString("Van_Sales_Cus_Position", "0").toString());
				}
			}
			
		}
		
		
		isRouteWise = true;
		
		RouteDS ds =new RouteDS(getActivity());
		lvCustomers = (ListView) view.findViewById(R.id.cus_lv);
		lvCustomers.clearTextFilter();
		dsDebtorDS = new DebtorDS(getActivity());
		customerList = dsDebtorDS.getRouteCustomers(ds.getRouteCodeByRoute(localSP.getString("Current_Route", "").toString()));
		lvCustomers.setAdapter(new CustomerAdapter(getActivity(), customerList));
		
		if(activity.selectedDebtor != null){
			lvCustomers.setSelection(activity.cusPosition);
		}
	
		
		lvCustomers.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view2,int position, long id) {
				MainActivity activity = (MainActivity) getActivity();
				debtor = customerList.get(position);
				activity.selectedDebtor = debtor;
				//activity.cusPosition=position;
				SharedPreferencesClass.setLocalSharedPreference(activity,"Van_Sales_Cus_Position",position+"");
				//Toast.makeText(getActivity(), debtor.getFDEBTOR_NAME() + " selected", Toast.LENGTH_SHORT).show();
				toast.createToastMessage( debtor.getFDEBTOR_NAME() + " selected", getActivity());
				getActivity().getActionBar().setSelectedNavigationItem(1);
			}
		});
		
		lvCustomers.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				MainActivity activity = (MainActivity) getActivity();
				debtor = customerList.get(position);
				activity.selectedDebtor = debtor;
				AlertBox alertBox=new AlertBox();
				alertBox.debtorDetailsDialogbox(getActivity(), debtor.getFDEBTOR_NAME(), debtor);
				
				return false;
			}
		});
		return view;
	}
	

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		for(int i = 0; i < menu.size(); ++i){
			menu.removeItem(menu.getItem(i).getItemId());
		}
		
		inflater.inflate(R.menu.frag_customer_phone_menu, menu);
		SearchView searchView = (SearchView) menu.findItem(R.id.action_customer_search_phone).getActionView();
		searchView.setOnQueryTextListener(new OnQueryTextListener() {
			
			@Override
			public boolean onQueryTextSubmit(String query) {
				return false;
			}
			
			@Override
			public boolean onQueryTextChange(String newText) {
				
				if(isRouteWise){
					
					RouteDS ds =new RouteDS(getActivity());
					lvCustomers = (ListView) view.findViewById(R.id.cus_lv);
					lvCustomers.clearTextFilter();
					dsDebtorDS = new DebtorDS(getActivity());
					customerList = dsDebtorDS.getRouteCustomersByCodeAndName(ds.getRouteCodeByRoute
							(localSP.getString("Current_Route", "").toString()),newText);
					lvCustomers.setAdapter(new CustomerAdapter(getActivity(), customerList));
					
					
				}else{
					
					lvCustomers = (ListView) view.findViewById(R.id.cus_lv);
					lvCustomers.clearTextFilter();
					dsDebtorDS = new DebtorDS(getActivity());
					customerList = dsDebtorDS.getCustomerByCodeAndName(newText);
					lvCustomers.setAdapter(new CustomerAdapter(getActivity(), customerList));
					
				}
				return false;
			}
		});
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	private void changeButtonAllCustomerOnPress(){
		allCust.setBackgroundColor(Color.parseColor("#525252"));
	}
	
	private void restButtonAllCustomerOnRelease(){
		allCust.setBackgroundColor(Color.parseColor("#9c9c9c"));
	}
	
	private void changeButtonRouteCustomerOnPress(){
		routeCust.setBackgroundColor(Color.parseColor("#525252"));
	}
	
	private void restButtonRouteCustomerOnPress(){
		routeCust.setBackgroundColor(Color.parseColor("#9c9c9c"));
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.all_cust:
			{
				//change button color
				changeButtonAllCustomerOnPress();
				restButtonRouteCustomerOnPress();
				
				isRouteWise =false;
				
				//fetch the all debtor 
				lvCustomers = (ListView) view.findViewById(R.id.cus_lv);
				lvCustomers.clearTextFilter();
				dsDebtorDS = new DebtorDS(getActivity());
				customerList = dsDebtorDS.getAllCustomers();
				customerAdapter = new CustomerAdapter(getActivity(), customerList);
				lvCustomers.setAdapter(customerAdapter);
				
				//toast.createToastMessage("All Debtor", getActivity());
			}	
		break;
		
		case R.id.route_wise_cust:
			{	
				//change button color
				changeButtonRouteCustomerOnPress();
				restButtonAllCustomerOnRelease();
				
				isRouteWise =true;
				
				//fetch the routewise debtor 
				RouteDS ds =new RouteDS(getActivity());
				lvCustomers = (ListView) view.findViewById(R.id.cus_lv);
				lvCustomers.clearTextFilter();
				dsDebtorDS = new DebtorDS(getActivity());
				customerList = dsDebtorDS.getRouteCustomers(ds.getRouteCodeByRoute(localSP.getString("Current_Route", "").toString()));
				lvCustomers.setAdapter(new CustomerAdapter(getActivity(), customerList));
				
				//toast.createToastMessage("Route Wise Debtor", getActivity());
			}	
		break;
		default:
			break;
		}
	}
	
	

}
